/*
jquery.drag-spinbox.js Create by TACHY/RAWSEQ
Homepage : http://ltside.com/dragspinbox/
How to setting : http://code.google.com/p/jquery-drag-spinbox/
*/

var dgsp = {};
dgsp.onspin = function( inputid, val ){}

jQuery.noConflict();
jQuery(document).ready(function($){
	var $j = jQuery;
	var dgspmat = document.createElement("div");
	dgsp.curId = false;
	dgsp.curX = 0;
	dgsp.curY = 0;
	dgsp.curVal = 0;
	dgsp.incrate = 1;
	dgsp.minval;
	dgsp.maxval;
	dgsp.magnif;
	
	dgsp.init = function(){
		dgsp.reset();
	
		$j(window).resize(function(){
			dgsp.remove();
			dgsp.reset();
		});

		$j(window).scroll(function(){
			$j(dgspmat).css("top",document.documentElement.scrollTop || document.body.scrollTop);
			$j(dgspmat).css("left",document.documentElement.scrollLeft || document.body.scrollLeft);
		});
	}
	
	dgsp.reset = function(){
		$j(dgspmat).append("").css({
			top: "0",
			left: "0",
			width: "100%",
			height: "100%",
			position: "absolute",
			zIndex: "99",
			background: "gray",
			cursor: "n-resize"
		}).html("").hide();
		$j("body").append(dgspmat);
		
		$j(dgspmat).mouseup(function(e){dgsp.turnoff();e.returnValue = false;return false;});
		$j(dgspmat).mouseout(function(e){dgsp.turnoff();e.returnValue = false;return false;});
		$j(dgspmat).mousemove(function(e){
			if(dgsp.curId != false ){
				if(dgsp.curX != 0 ){
					dgsp.incrate = parseInt((e.pageX - dgsp.curX)/50);
					dgsp.incrate = (dgsp.incrate * dgsp.magnif) +1;
				}else{
					dgsp.curX = e.pageX;
				}
				
				if(dgsp.curY != 0){
					var inc = (parseInt(dgsp.curY) - parseInt(e.pageY)) * parseInt(dgsp.incrate);
					dgsp.curVal = dgsp.curVal + inc;
					if( dgsp.minval!=dgsp.maxval ){
						if((dgsp.curVal + inc) < dgsp.minval) dgsp.curVal = dgsp.minval;
						if((dgsp.curVal + inc) > dgsp.maxval) dgsp.curVal = dgsp.maxval;
					}
					$j("#"+dgsp.curId).attr("value", dgsp.curVal);
					dgsp.onspin(dgsp.curId,dgsp.curVal);
				}
				
				dgsp.curY = e.pageY;
			}
			e.returnValue = false;
			return false;
		});
		
		var tginput = $j("input[type='text']:visible")
		.filter(function(){
			if(this.className.match(/dgsp-/) 
				&& $j(this).height() != 0 ) {
				return true;
			}else{
				return false;
			}
		});
	
		tginput.each(function(){
			var inputclass = this.className.replace(/^.*dgsp-([^ ]+).*$/,"$1");
			var box =  $j(".dgsp-"+inputclass+"-b");
			box.css('cursor','n-resize');
			var inputid = this.id;
			box.mousedown(function(e){
				dgsp.curId = inputid;
				dgsp.curVal = parseInt($j("#"+dgsp.curId).val());
				if(isNaN(dgsp.curVal)) dgsp.curVal = 0;
				
				dgsp.minval = parseInt($("#"+inputid).attr("class").replace(/^.*dgspmin-([0-9\-]+).*$/,"$1"));
				dgsp.maxval = parseInt($("#"+inputid).attr("class").replace(/^.*dgspmax-([0-9\-]+).*$/,"$1"));
				dgsp.magnif = parseInt($("#"+inputid).attr("class").replace(/^.*dgspmgn-([0-9\-]+).*$/,"$1"));
				
				if(isNaN(dgsp.minval)) dgsp.minval=0;
				if(isNaN(dgsp.maxval)) dgsp.maxval=0;
				if(isNaN(dgsp.magnif)) dgsp.magnif=5;
				
				$j(dgspmat).fadeTo(0,0).show();
				e.returnValue = false;
				return false;
			});
			box.mousemove(function(e){e.returnValue = false;return false});
			box.mouseup(function(e){dgsp.turnoff(); e.returnValue = false; return false;});
		});
	}
	
	dgsp.turnoff = function(){
		$j(dgspmat).hide();
		dgsp.curX = 0;
		dgsp.curY = 0;
		dgsp.curId = false;
		dgsp.incrate = 1;
	}
	
	dgsp.remove = function(){
		$j(dgspmat).remove();
	}

	dgsp.init();

});

